$(function () {
    //  按钮生成器
    generateButton($('#north'));
    let columns = [[
        {
            field: 'id',
            title: '编号',
            width: 100,
            align: 'center',
            halign: 'center',
            sortable: true,
            editor: {type: 'textbox', options: {required: true}}
        },
        {
            field: 'storeName',
            title: '名称',
            width: 200,
            align: 'center',
            halign: 'center',
            sortable: true,
            editor: {type: 'textbox', options: {required: true}}
        },
        {
            field: 'isMaterial',
            title: '是否材料仓库',
            width: 100,
            align: 'center',
            halign: 'center',
            sortable: true,
            formatter: (v, r, i) => v ? '是' : '否',
            editor: {type: 'checkbox', options: {on: true, off: false}}
        },
        {
            field: 'isSemiFinished',
            title: '是否半成品仓库',
            width: 100,
            align: 'center',
            halign: 'center',
            sortable: true,
            formatter: (v, r, i) => v ? '是' : '否',
            editor: {type: 'checkbox', options: {on: true, off: false}}
        },
        {
            field: 'isProduct',
            title: '是否成品仓库',
            width: 100,
            align: 'center',
            halign: 'center',
            sortable: true,
            formatter: (v, r, i) => v ? '是' : '否',
            editor: {type: 'checkbox', options: {on: true, off: false}}
        },
    ]]
    setDatagridColumnOpts($('body').data('menu_id'), $('#e_grid'), columns, edatagrid);
    bindEnterEvents()
})

function edatagrid(columns) {
    $('#e_grid').edatagrid({
        method: 'GET',
        param: {},
        url: 'jcStore/listQuery',
        saveUrl: 'jcStore/insertSelective',
        updateUrl: 'jcStore/updateSelective',
        destroyUrl: 'jcStore/deleteByPk',
        idField: 'id',
        destroyMsg: {
            norecord: {	// when no record is selected
                title: '警告',
                msg: '未选中任何数据'
            },
            confirm: {	// when select a row
                title: '确认',
                msg: '确定删除吗?删除后不可恢复!'
            }
        },
        loadFilter: pageFilter,
        pagination: true,
        // pagePosition: 'top',
        pageSize: 200,
        pageNumber: 1,
        pageList: [200, 400, 600, 800, 1000],
        striped: true,
        fit: true,
        sortable: true,
        rownumbers: true,
        remoteSort: false,
        showFooter: true,
        singleSelect: true,
        columns: columns,
        onLoadSuccess: function () {
            $(this).datagrid('enableFilter');
            $(this).data('edatagrid').options.editIndex = -1;
            $('.easyui-linkbutton').each(function () {
                if ($(this).linkbutton('options').text == '查询') {
                    $(this).linkbutton('enable');
                }
            })
        },
        onBeforeLoad: function (param) {
            $(this).datagrid('destroyFilter');
            $('.easyui-linkbutton').each(function () {
                if ($(this).linkbutton('options').text == '查询') {
                    $(this).linkbutton('disable');
                }
            })
            return true;
        },
        onSuccess: function (index, row) {
            $(this).edatagrid('reload');
            $('.easyui-linkbutton').each(function () {
                if ($(this).linkbutton('options').text == '查询') {
                    $(this).linkbutton('enable');
                }
            })
            if (row.success) {
                $.messager.show({title: '提示', msg: row.message});
            } else {
                $(this).edatagrid('enableEditing');
                $.messager.alert('提示', row.message, 'warning')
            }
        },
        onDestroy: function (index, row) {
            $(this).edatagrid('reload');
            if (row.success) {
                $.messager.show({title: '提示', msg: row.message});
            } else {
                $.messager.alert('提示', row.message, 'warning')
            }
        },
        onError: function (index, row) {
            $.messager.show({title: '提示', msg: row.message});
        },
        onResizeColumn: function (f, w) {
            columnResizeEvent($('body').data('menu_id'), f, w);
        },
        onEdit: function (index, row) {
            let editors = $(this).edatagrid('getEditors', index);
            let idField = $(this).edatagrid('options').idField;
            for (let editor of editors) {
                if (editor.field == idField) {
                    $(editor.target).textbox('readonly');
                }
            }
        }
    })
}

function btn_search() {
    $('#e_grid').edatagrid('reload');
}

function btn_add() {
    $('#e_grid').edatagrid('addRow');
}

function btn_modify() {
    let selected = $('#e_grid').edatagrid('getSelected');
    if (selected != undefined) {
        let index = $('#e_grid').edatagrid('getRowIndex', selected);
        if (index != -1) {
            $('#e_grid').edatagrid('editRow', index);
        }
    } else {
        $.messager.alert('提示', '点击鼠标左键选中行后再执行本功能', 'warning');
    }
}

function btn_delete() {
    let selected = $('#e_grid').edatagrid('getSelected');
    if (selected != undefined) {
        let index = $('#e_grid').edatagrid('getRowIndex', selected);
        if (index != -1) {
            $('#e_grid').edatagrid('destroyRow', index);
        }
    } else {
        $.messager.alert('提示', '点击鼠标左键选中行后再执行本功能', 'warning');
    }
}

function btn_import() {

}

function btn_save() {
    $('#e_grid').edatagrid('saveRow');
}

function btn_export() {
    exportExcel('#e_grid', '仓库');
}